package com.alingrad.android.froyoengine.graphics;

import javax.microedition.khronos.opengles.GL10;

import com.alingrad.android.froyoengine.graphics.renderable.Sprite;
import com.alingrad.android.froyoengine.tree.TreeNode;

/**
 * Abstract class that contains all the animations for a particular object.
 * Must be overridden or defined in each object's class definition.
 * @author alingrad
 *
 */

public class FrameByFrameAnimationHandler extends TreeNode {
	/**
	 * Array of all animations for an object.
	 */
	protected FrameByFrameAnimation[] mAnimations;

	/**
	 * Index of the current animation in the animation array.
	 */
	private int mCurrentAnim;

	/**
	 * Constructs an AnimationHandler.
	 */
	public FrameByFrameAnimationHandler(FrameByFrameAnimation... animations) {
		super();
		mAnimations = animations;
	}

	/**
	 * method that returns the current sprite to be used.
	 * @return the sprite.
	 */
	public Sprite getCurrentSprite() {
		return mAnimations[mCurrentAnim].getCurrentSprite();
	}

	/**
	 * Sets the current animation. Will not reset if the animation does not change.
	 * @param animation the animation to set.
	 */
	public void setAnimation(final int animation) {
		if (mCurrentAnim != animation) {
			resetAnimation(animation);
		}
	}

	/**
	 * Continues an animation from its last point.
	 * @param animation animation to continue.
	 */
	public void continueAnimation(final int animation) {
		mCurrentAnim = animation;
	}

	/**
	 * Sets and resets the current animation, even if its the same animation.
	 * @param animation animation to reset.
	 */
	public void resetAnimation(final int animation) {
		mCurrentAnim = animation;
		mAnimations[mCurrentAnim].reset();
	}

	/**
	 * Returns the current animation being used.
	 * @return the current animation.
	 */
	public int getCurrentAnimation() {
		return mCurrentAnim;
	}

	/**
	 * Updates to the correct animation.
	 * @param delta Time (in milliseconds) since the last update call.
	 */
	@Override
	public void update(final long delta) {
		mAnimations[mCurrentAnim].update(delta);
	}

	@Override
	public void load(GL10 gl) {
	}

	@Override
	public boolean isAlive() {
		return true;
	}
}
